package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.Friend;
import model.User;

public class FriendData {
	public static void addFriend(String email, String emailFriend) {
		Connection con = DbConnection.getConnect();
		String sql = "INSERT INTO friend VALUES(?,?,?)";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, email);
			pre.setString(2, emailFriend);
			pre.setString(3, "0");
			pre.executeUpdate();
			pre.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void addFriendN(String email, String emailFriend) {
		Connection con = DbConnection.getConnect();
		String sql = "INSERT INTO friend VALUES(?,?,?)";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, emailFriend);
			pre.setString(2, email);
			pre.setString(3, "0");
			pre.executeUpdate();
			pre.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void updateType(String email, String emailFriend, String type) {
		Connection con = DbConnection.getConnect();
		String sql = "UPDATE friend SET type = ? WHERE user_email= ? AND emailFriend =?";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, type);
			pre.setString(2, email);
			pre.setString(3, emailFriend);
			pre.executeUpdate();
			pre.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static void updateTypeN(String email, String emailFriend, String type) {
		Connection con = DbConnection.getConnect();
		String sql = "UPDATE friend SET type = ? WHERE user_email= ? AND emailFriend =?";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, type);
			pre.setString(2, emailFriend);
			pre.setString(3, email);
			pre.executeUpdate();
			pre.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static void unFriend(String email, String emailFriend) {
		Connection con = DbConnection.getConnect();
		String sql = "DELETE FROM friend WHERE user_email= ? AND emailFriend =?";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, email);
			pre.setString(2, emailFriend);
			pre.executeUpdate();
			pre.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static ArrayList<Friend> getListFriend(String email) {
		ArrayList<Friend> list = new ArrayList<>();
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM friend WHERE user_email= ? ";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, email);
			ResultSet rs = pre.executeQuery();
			while (rs.next()) {
				String emailFriend = rs.getString("emailFriend");
				String type = rs.getString("type");
				Friend friend = new Friend(email, emailFriend, type);
				list.add(friend);
			}
			rs.close();
			pre.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;

	}

	public static boolean checkFriend(String email, String emailFriend) {
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM friend WHERE user_email= ? AND emailFriend = ? ";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, email);
			pre.setString(2, emailFriend);
			ResultSet rs = pre.executeQuery();
			if (rs.next())
				return true;
			else
				return false;

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;

	}

	public static void addResquesFriend(String user_email, String recieve) {
		Connection con = DbConnection.getConnect();
		String sql = "INSERT INTO resquesAddFriend VALUES(?,?)";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, user_email);
			pre.setString(2, recieve);
			pre.executeUpdate();
			pre.close();
			con.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void delReques(String email, String recieve) {
		Connection con = DbConnection.getConnect();
		String sql = "DELETE FROM friend WHERE user_email= ? AND recieve =?";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, email);
			pre.setString(2, recieve);
			pre.executeUpdate();
			pre.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static boolean checkResques(String email, String recieve) {
		Connection con = DbConnection.getConnect();
		String sql = "select * FROM resquesAddFriend WHERE user_email= ? AND recieve =?";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, email);
			pre.setString(2, recieve);
			ResultSet rs = pre.executeQuery();
			if (rs.next()) {
				pre.close();
				con.close();
				return true;
			}

			else {
				pre.close();
				con.close();
				return false;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;

	}

	public static ArrayList<String> getResques(String recieve) {
		ArrayList<String> list = new ArrayList<>();
		Connection con = DbConnection.getConnect();
		String sql = "SELECT * FROM resquesAddFriend WHERE recieve= ? ";
		PreparedStatement pre;

		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, recieve);
			ResultSet rs = pre.executeQuery();
			while (rs.next()) {
				String emailFriend = rs.getString("user_email");
				list.add(emailFriend);
			}
			rs.close();
			pre.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;

	}

	public static void main(String[] args) {
		User u = UserData.getUser("hoangipok@zing.vn");
		System.out.println(u.getMoney());
	}

}
